package wang.z.JFrame;

import wang.z.component.BackGroundPanel;
import wang.z.model.ResultInfo;
import wang.z.net.FailListener;
import wang.z.net.ImageRequestUtils;
import wang.z.net.PostUtils;
import wang.z.net.SuccessListener;
import wang.z.utils.JsonUtils;
import wang.z.utils.PathUtils;
import wang.z.utils.ScreenUtils;
import wang.z.utils.toolUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class last {
    JFrame jf = new JFrame("注册");

    final int WIDTH = 600;
    final int HEIGHT = 500;
    boolean zFlag = false;
    boolean pFlag = false;
    boolean cFlag = false;
    boolean uFlag = false;
    JLabel uLabel;
    JLabel userNameMag;
    JTextField uField;
    JLabel pLabel;
    JLabel passwordMes;
    JPasswordField  pField;

    JLabel tLabel;
    JLabel phoneMag;
    JTextField tField;

    JLabel cLabel;
    JLabel codeMag;
    JTextField cField;


    //组装视图
    public void init() throws Exception {

        //设置窗口的属性
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        jf.setResizable(false);
        jf.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("logo.png"))));

        //用户名
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0,0,560,360);
        uLabel = new JLabel("用户名：");
        uLabel.setBounds(80,40,80,27);
        uLabel.setFont(new Font("幼圆", Font.BOLD, 14));
        uField = new JTextField();
        uField.setBounds(140,40,250,27);
        userNameMag = new JLabel("");
        userNameMag.setBounds(390,40,200,27);
        userNameMag.setFont(new Font("幼圆", Font.BOLD, 14));
        panel.add(uLabel);
        panel.add(uField);
        panel.add(userNameMag);
        uField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
            }

            @Override
            public void focusLost(FocusEvent e) {
                user();

            }
        });


        //组装密码
        panel.setBounds(0,0,560,360);
         pLabel = new JLabel("密  码：");
        pLabel.setBounds(80,90,80,27);
        pLabel.setFont(new Font("幼圆", Font.BOLD, 14));
        pField = new JPasswordField();
        pField.setBounds(140,90,250,27);
        passwordMes = new JLabel("");
        passwordMes.setBounds(390,90,200,27);
        passwordMes.setFont(new Font("幼圆", Font.BOLD, 14));
        panel.add(pLabel);
        panel.add(pField);
        panel.add(passwordMes);



        //组装手机号

        panel.setBounds(0,0,560,360);
        tLabel = new JLabel("手机号：");
        tLabel.setBounds(80,140,80,27);
        tLabel.setFont(new Font("幼圆", Font.BOLD, 14));

        tField = new JTextField();
        tField.setBounds(140,140,250,27);

        phoneMag = new JLabel("");
        phoneMag.setBounds(390,140,200,27);
        phoneMag.setFont(new Font("幼圆", Font.BOLD, 14));

        panel.add(tLabel);
        panel.add(tField);
        panel.add(phoneMag);


        pField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
            }

            @Override
            public void focusLost(FocusEvent e) {
                password();

            }
        });
        tField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
            }

            @Override
            public void focusLost(FocusEvent e) {
                phone();

            }
        });

        //组装性别

        JLabel gLabel = new JLabel("性  别：");
        gLabel.setBounds(80,200,80,27);
        gLabel.setFont(new Font("幼圆", Font.BOLD, 14));

        JRadioButton maleBtn = new JRadioButton("男", true);
        maleBtn.setBounds(140,200,40,27);
        JRadioButton femaleBtn = new JRadioButton("女", false);
        femaleBtn.setBounds(180,200,40,27);
        //实现单选的效果
        ButtonGroup bg = new ButtonGroup();
        bg.add(maleBtn);
        bg.add(femaleBtn);
        panel.add(gLabel);
        panel.add(maleBtn);
        panel.add(femaleBtn);

         cLabel = new JLabel("验证码：");
        cLabel.setBounds(80,250,80,27);
        cLabel.setFont(new Font("幼圆", Font.BOLD, 14));

         cField = new JTextField();
        cField.setBounds(140,250,150,27);
         codeMag = new JLabel("");
        cField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
            }

            @Override
            public void focusLost(FocusEvent e) {
               code();
            }
        });

        codeMag.setBounds(290,250,200,27);
        codeMag.setFont(new Font("幼圆", Font.BOLD, 14));


        panel.add(codeMag);
        panel.add(cField);
        panel.add(cLabel);
        JLabel cImg = new JLabel(new ImageIcon(ImageRequestUtils.getImage("http://localhost:8080/code/getCheckCode")));
        //给某个组件设置提示信息
        cImg.setBounds(320,250,100,27);
        panel.add(cImg);
        cImg.setToolTipText("点击刷新");
        cImg.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                cImg.setIcon(new ImageIcon(ImageRequestUtils.getImage("http://localhost:8080/code/getCheckCode")));
                cImg.updateUI();
            }
        });

        JButton registBtn = new JButton("注册");
        registBtn.setBounds(120,320,100,27);
        registBtn.setFont(new Font("幼圆", Font.BOLD, 14));
        JButton backBtn = new JButton("返回登录页面");
        backBtn.setBounds(320,320,150,27);
        backBtn.setFont(new Font("幼圆", Font.BOLD, 14));

        panel.add(registBtn);
        panel.add(backBtn);

        registBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if(!(uFlag&pFlag&cFlag&zFlag)){
                    user();
                    password();
                    phone();
                    code();
                    JOptionPane.showMessageDialog(jf, "信息不正确，请检查");

                    return;
                }

                //获取用户录入的信息
                String username = uField.getText().trim();
                String password = pField.getText().trim();
                String phone = tField.getText().trim();
                String sex = bg.isSelected(maleBtn.getModel()) ? maleBtn.getText() : femaleBtn.getText();
                String checkCode = cField.getText().trim();
                Map<String, String> params = new HashMap<>();
                params.put("username", username);
                params.put("password", password);
                params.put("phone", phone);
                params.put("sex", sex);
                params.put("role", "0");
                params.put("checkCode", checkCode);
                //访问后台接口
                PostUtils.postWithParams("http://localhost:8080/user/register", params, new SuccessListener() {
                    @Override
                    public void success(String result) {
                        ResultInfo info = JsonUtils.parseResult(result);
                        if (info.isFlag()) {
                            //注册成功
                            JOptionPane.showMessageDialog(jf, "注册成功，即将返回登录页面");
                            try {
                                new loginFrm().init();
                                jf.dispose();
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        } else {
                            if(info.getMessage().equals("重复用户名，注册失败")){
                                pFlag = false;
                                userNameMag.setText("用户名已经存在");
                                userNameMag.setForeground(Color.RED);
                            }
                            if(info.getMessage().equals("验证码错误")){
                                cFlag = false;
                                codeMag.setText("错误");
                                codeMag.setForeground(Color.RED);
                            }
                            if(info.getMessage().equals("请刷新验证码")){
                                cFlag = false;
                                codeMag.setText("请刷新");
                                codeMag.setForeground(Color.RED);
                            }



                            //注册失败

                        }
                    }
                }, new FailListener() {
                    @Override
                    public void fail() {
                        JOptionPane.showMessageDialog(jf, "网络异常，请稍后重试");
                    }
                });
            }
        });

        backBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //返回到登录页面即可
                try {
                    new loginFrm().init();
                    jf.dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        panel.setBackground(null);
        panel.setOpaque(false);
        jf.add(panel);
        BackGroundPanel bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("regist.jpg"))));
        bgPanel.setBounds(0, 0, WIDTH, HEIGHT);
        jf.add(bgPanel);


        jf.setVisible(true);
    }

    private void code() {
        String code = cField.getText();

        if (toolUtil.isEmpty(code)) {
            cFlag = false;
            codeMag.setText("空");
            codeMag.setForeground(Color.RED);
        } else {
            cFlag = true;
            codeMag.setText("");
        }
    }

    private void phone() {
        String phone = tField.getText();

        if (toolUtil.isEmpty(phone)) {

            pFlag = false;
            phoneMag.setText("手机号不能为空");
            phoneMag.setForeground(Color.RED);
        } else {
            boolean flag = toolUtil.isPhoneLegal(phone);
            if (flag) {
                pFlag = true;
                phoneMag.setText("√");
                phoneMag.setForeground(Color.GREEN);
            } else {
                pFlag = false;
                phoneMag.setText("请输入正确的手机号");
                phoneMag.setForeground(Color.RED);
            }
        }
    }

    private void password() {

        String pwd = pField.getText();

        if (toolUtil.isEmpty(pwd)) {
            zFlag = false;
            passwordMes.setText("密码不能为空");
            passwordMes.setForeground(Color.RED);
        } else {
            boolean flag = pwd.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
            if (flag) {
                zFlag = true;
                passwordMes.setText("√");
                passwordMes.setForeground(Color.GREEN);
            } else {
                zFlag = false;
                passwordMes.setText("16位数字和字母的组合");
                passwordMes.setForeground(Color.RED);
            }
        }
    }

    private void user() {
        String code = uField.getText();

        if (toolUtil.isEmpty(code)) {
            uFlag = false;
            userNameMag.setText("用户名不能为空");
            userNameMag.setForeground(Color.RED);
        } else {
            userNameMag.setText("");
            uFlag = true;

        }
    }




}
